package com.example.add8_17;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Button mBu;
    private ImageView mImage;
    private TextView mName;
    private int a=6;
    private int msg1=0;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what==0){
                a--;
                mName.setText("倒计时"+a+"s");
                if (a==0){
                    msg1=1;
                    initIntent();
                }
                handler.sendEmptyMessageDelayed(msg1,1000);
            }
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    private void initViews() {
        mBu = findViewById(R.id.bu);
        mImage = findViewById(R.id.image);
        mName = findViewById(R.id.name);
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim);
        mImage.startAnimation(animation);
        handler.obtainMessage();
        mBu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initIntent();
            }
        });
        handler.sendEmptyMessageDelayed(msg1,1000);
    }

    private void initIntent() {
        Intent intent = new Intent(this, Main2Activity.class);
        startActivity(intent);
    }

}
